package pei.lei.lostandfound.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import pei.lei.lostandfound.base.page.PageList;
import pei.lei.lostandfound.base.page.Paginator;
import pei.lei.lostandfound.base.service.impl.BaseServiceImpl;
import pei.lei.lostandfound.base.utils.NoticeStateCode;
import pei.lei.lostandfound.dto.NoticeDTO;
import pei.lei.lostandfound.entity.Notice;
import pei.lei.lostandfound.mapper.NoticeMapper;
import pei.lei.lostandfound.service.NoticeService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Zhang
 * @since 2021-01-18
 */
@Service
public class NoticeServiceImpl extends BaseServiceImpl<NoticeMapper, Notice> implements NoticeService {


        @Autowired
        private NoticeMapper noticeMapper;


    @Override
    public List<NoticeDTO> FuzzyQuery(String message) {
        message = "%"+message+"%";
        return noticeMapper.FuzzyQuery(message);

    }

    @Override
    public PageList<NoticeDTO> getNewNotice(int pageNum, int pageSize) {
        List<NoticeDTO> noticeDTOList = noticeMapper.getNewNotice();
/*        Page<NoticeDTO> result = PageHelper.startPage(pageNum, pageSize);*/
        Paginator paginator = new Paginator(pageNum, pageSize, (int) noticeDTOList.size());
        PageList<NoticeDTO> pageList = new PageList<NoticeDTO>(paginator);
        //分页
        List<NoticeDTO> returnList = new ArrayList<>();
        if (noticeDTOList != null && noticeDTOList.size() > 0) {
            int currIdx = (pageNum > 1 ? (pageNum - 1) * pageSize : 0);
            for (int i = 0; i < pageSize && i < noticeDTOList.size() - currIdx; i++) {
                NoticeDTO data = noticeDTOList.get(currIdx + i);
                returnList.add(data);
            }
        }
        pageList.setData(returnList);
        return pageList;
    }
}